package com.xxgc.moriserver.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xxgc.moriserver.entity.FriendEntity;
import com.xxgc.moriserver.entity.FriendEntityRepository;
import com.xxgc.moriserver.entity.UserEntity;
import com.xxgc.moriserver.entity.UserEntityRepository;
import com.xxgc.moriserver.result.Result;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取用户信息
 */
@RestController
@RequestMapping("/users")
@Tag(name = "用户信息")
public class UserInfoController {
    @Autowired
    private UserEntityRepository userEntityRepository;

    @Autowired
    private FriendEntityRepository friendEntityRepository;

    @GetMapping("/getUserInfo")
    public Result getUserInfo(){
        //获取用户的id
        int loginIdAsInt = StpUtil.getLoginIdAsInt();
        System.out.println("loginIdAsInt = " + loginIdAsInt);
        UserEntity usersEntity = userEntityRepository.getById(loginIdAsInt);
        return Result.success(usersEntity);
    }

    //添加好友
    @PostMapping("/addFriend")
    public Result addFriend(@RequestBody Map map){
        String nickName = (String) map.get("nickName");
        //1 判断是否有这个用户
        QueryWrapper<UserEntity> qw = new QueryWrapper<>();
        qw.eq("nick_name",nickName);
        UserEntity one = userEntityRepository.getOne(qw);
        // 判断
        if(one == null){
            return Result.fail("没有这个用户");
        }else{
            //2 判断是否已经是好友
            QueryWrapper<FriendEntity> qw1 = new QueryWrapper<>();
            //token获取的自己的id
            qw1.eq("friend_user_id",StpUtil.getLoginIdAsInt());
            qw1.eq("friend_id",one.getUserId());
            FriendEntity one1 = friendEntityRepository.getOne(qw1);
            if(one1 != null){
                return Result.fail("已经是好友或已发送请求");
            }else{
                //3 发生添加好友的请求
                FriendEntity friendEntity = new FriendEntity();
                friendEntity.setFriendUserId(StpUtil.getLoginIdAsString());
                friendEntity.setFriendId(one.getUserId());
                friendEntity.setStatus(0);
                friendEntityRepository.save(friendEntity);
                return Result.success("发送成功");
            }
        }
    }

    // 获取待添加的好友列表 Status为0的
    @GetMapping("/getFriendList")
    public Result getFriendList(){
        QueryWrapper<FriendEntity> qw = new QueryWrapper<>();
        qw.eq("friend_id",StpUtil.getLoginIdAsInt());
        qw.eq("status",0);
        List<FriendEntity> list = friendEntityRepository.list(qw);
        if (list.isEmpty()) {
            return Result.fail(800,"没有添加的用户");
        }
        Map map=new HashMap<>();
        System.out.println("list = " + list);

        for (FriendEntity friendEntity : list) {
                String getFriendId = friendEntity.getFriendUserId();
                UserEntity usersEntity = userEntityRepository.getById(getFriendId);
                FriendEntity friend = friendEntity; // 获取FriendEntity元素
                friend.setUsersEntity(usersEntity);
                map.put("friendsList：",friend);
        }
        if (map.isEmpty()) {
            return Result.fail(800,"map没有添加的用户");
        }

        return Result.success(map);
    }

    @GetMapping("/getFriendListTrue")
    public Result getFriendListTrue(){
        QueryWrapper<FriendEntity> qw = new QueryWrapper<>();
        qw.eq("friend_user_id",StpUtil.getLoginIdAsInt());
        qw.eq("status",1);
        List<FriendEntity> list = friendEntityRepository.list(qw);
        if (list.isEmpty()) {
            return Result.fail(800,"没有已经添加的用户");
        }

        for (FriendEntity friendEntity : list) {
            String getFriendId = friendEntity.getFriendId();
            UserEntity usersEntity = userEntityRepository.getById(getFriendId);
            FriendEntity friend = friendEntity; // 获取FriendEntity元素
            friend.setUsersEntity(usersEntity);
        }

        return Result.success(list);
    }


    @PostMapping("/agreeFriends")
    public Result agreeFriends(@RequestBody Map map){
        String friendId= (String)map.get("friendId");
        String friendUserId= (String)map.get("friendUserId");

        UpdateWrapper<FriendEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .eq("friend_id", friendId)  // 指定修改条件
                .eq("friend_user_id",friendUserId)
                .set("status", 1);// 设置新值
        boolean update = friendEntityRepository.update(updateWrapper);

        QueryWrapper<FriendEntity> qw1 = new QueryWrapper<>();
        //查看对面也是否加自己好友如果有给对面自己发的好友请求同意
        qw1.eq("friend_user_id",friendId);
        qw1.eq("friend_id",friendUserId);
        FriendEntity one1 = friendEntityRepository.getOne(qw1);
        System.out.println("请求"+one1);
        if(one1 == null){
            System.out.println("没有自己给对面的好友请求");
        }else{
            UpdateWrapper<FriendEntity> updateWrapper2 = new UpdateWrapper<>();
            updateWrapper2
                    .eq("friend_id", friendUserId)  // 指定修改条件
                    .eq("friend_user_id",friendId)
                    .set("status", 1);// 设置新值
            boolean update2 = friendEntityRepository.update(updateWrapper2);//一边同意另一边的也同样修改
            if (update2){
                System.out.println("另一边的好友请求修改成功"+update2);
            }
        }

        if(update){
            return Result.success();
        }

        return Result.fail(500,"同意好友请求失败");
    }

    @PostMapping("/disagreeFriends")
    public Boolean disagreeFriends(String friendId){
        QueryWrapper<FriendEntity> qw = new QueryWrapper<>();
        qw.eq("friend_id",friendId);
        qw.eq("status",0);
        boolean delete = friendEntityRepository.remove(qw);
        return delete;
    }
}
